<template>
  <div class="homepage-hero-module">
  <div class="video-container">
    <div :style="fixStyle" class="filter">
      <div style="width:400px ;margin: 150px auto">
        <div style="color:#e0f8a7;font-size: 30px;font-weight: bold ;text-align: center;padding: 30px 0 ">
          欢迎登录
        </div>
        <div style="color:#e0f8a7;font-size: 10px;font-weight: bold ;text-align: center;padding: 10px 0 ">
          powered by jiazi
        </div>
        <el-icon style="vertical-align: middle;">
          <search/>
        </el-icon>
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="用户名" v-model="form.username">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-user" placeholder="密码" v-model="form.password"
                      show-password></el-input>
          </el-form-item>



          <el-form-item>
            <div style="display: flex">
              <el-input prefix-icon="el-icon-key" v-model="form.validCode" style="width: 50%;" placeholder="请输入验证码"></el-input>
              <ValidCode @input="createValidCode" ></ValidCode>
            </div>
          </el-form-item>
          <el-form-item>
            <el-radio v-model="form.age" :label="3" style="color: rosybrown">管理员</el-radio>
            <el-radio v-model="form.age" :label="1" style="color: fuchsia">教师</el-radio>
            <el-radio v-model="form.age" :label="2" style="color: slateblue">学生</el-radio>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%" type="primary" @click="login">登录</el-button>
          </el-form-item>

          <el-form-item>
            <el-button style="width: 100%" type="primary" @click="register">没有账号，点我注册</el-button>
          </el-form-item>

          <el-form-item>
            <el-button style="width: 100%" type="primary" @click="findpassword">忘记密码，点我找回</el-button>
          </el-form-item>

        </el-form>
    </div>
  </div>
    <video :style="fixStyle" autoplay loop muted class="fillWidth" v-on:canplay="canplay">
      <source src="../components/view.mp4" type="video/mp4"/>
      浏览器不支持 video 标签，建议升级浏览器。
    </video>
  </div>
  </div>
</template>

<script>
import request from "../utils/request";
import ValidCode from "../components/ValidCode";
export default {

  name: "Login",
  components:{
    ValidCode
  },
  data() {
    return {
      vedioCanPlay: false,
      fixStyle: '',
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: "请输入用户名", trigger: 'blur'},
        ],
        password: [
          {required: true, message: "请输入密码", trigger: 'blur'},
        ],
      }
    }

  },
  created(){
    sessionStorage.removeItem("user")
  },
  mounted() {
    sessionStorage.removeItem("user")

    window.onresize = () => {
      const windowWidth = document.body.clientWidth
      const windowHeight = document.body.clientHeight
      const windowAspectRatio = windowHeight / windowWidth
      let videoWidth
      let videoHeight
      if (windowAspectRatio < 0.5625) {
        videoWidth = windowWidth
        videoHeight = videoWidth * 0.5625
        this.fixStyle = {
          height: windowWidth * 0.5625 + 'px',
          width: windowWidth + 'px',
          'margin-bottom': (windowHeight - videoHeight) / 2 + 'px',
          'margin-left': 'initial'
        }
      } else {
        videoHeight = windowHeight
        videoWidth = videoHeight / 0.5625
        this.fixStyle = {
          height: windowHeight + 'px',
          width: windowHeight / 0.5625 + 'px',
          'margin-left': (windowWidth - videoWidth) / 2 + 'px',
          'margin-bottom': 'initial'
        }
      }
    }
    window.onresize()
  },
  methods: {
    canplay() {
      this.vedioCanPlay = true
    },
    register() {
      this.$router.push("/register")
    },
    findpassword(){
      this.$message.warning("您的权限不够，请联系管理员修改密码 ,   联系方式:17516728726")

    },
    createValidCode(data){
      this.validCode=data;
    },
    login() {

      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (!this.form.validCode) {
            this.$message.error("请填写验证码")
            return
          }
          if(this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
            this.$message.error("验证码错误")
            return
          }
          request.post("/user/login", this.form).then(res => {
            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "登录成功"
              })
              sessionStorage.setItem("user", JSON.stringify(res.data))  // 缓存用户信息

              // 登录成功的时候更新当前路由
              /*activeRouter()*/
              this.$router.push("/person")  //登录成功之后进行页面的跳转，跳转到主页

            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
          })
        }
      })
    }
  }

}
</script>
<style scoped>
.homepage-hero-module,
.video-container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.video-container .poster img{
  z-index: 0;
  position: absolute;
}

.video-container .filter {
  z-index: 1;
  position: absolute;
  /*background: rgba(0, 0, 0, 0.4);*/
  width: 100%;
}

.fillWidth {
  width: 100%;
}
</style>
